// Package product
// Author liuzhiyong
// Date 2023/11/15
// Description 商品控制器
package product

import (
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"net/http"
	"shopping/service"
	"shopping/utils/api_helper"
	"shopping/utils/pagination"
)

// Controller
// Description: 商品控制器结构体
type Controller struct {
	productService *service.ProductService
}

// NewProductController
// Description: 实例化商品控制器
// param s 商品service
// return *Controller 控制器
func NewProductController(s *service.ProductService) *Controller {
	return &Controller{
		productService: s,
	}
}

// GetProducts 获取商品列表
// @Summary 获得商品列表(分页)
// @Tags Product
// @Produce json
// @param qt query string false "Search text to find matched sku numbers and names"
// @param page query int false "page number"
// @param pageSize query int false "page size"
// @Success 200 {object} pagination.Pages
// @Router /product [get]
func (c *Controller) GetProducts(g *gin.Context) {
	page := pagination.NewFormGinRequest(g, -1)
	queryText := g.Query("qt")
	if queryText != "" {
		page = c.productService.SearchProduct(queryText, page)
	} else {
		page = c.productService.GetAll(page)
	}
	g.JSON(http.StatusOK, page)
}

// CreateProduct 创建商品
// @Summary 创建商品
// @Tags Product
// @Accept json
// @Produce json
// @param Authorization header string true "Authorization header"
// @param CreateProductRequest body CreateProductRequest true "product information"
// @Success 200 {object} api_helper.Response
// @Failure 400 {object} api_helper.ErrorResponse
// @Router /product [post]
func (c *Controller) CreateProduct(g *gin.Context) {
	var req CreateProductRequest
	if err := g.ShouldBind(&req); err != nil {
		// 解析参数错误
		api_helper.HandleError(g, err)
		return
	}
	err := c.productService.CreateProduct(req.Name, req.Desc, req.Count, decimal.NewFromFloat32(req.Price), req.CategoryId)
	if err != nil {
		api_helper.HandleError(g, err)
		return
	}
	g.JSON(http.StatusCreated, api_helper.Response{
		Message: "Product Created",
	})
}

// DeleteProduct 删除商品
// @Summary 删除商品根据sku
// @Tags Product
// @Accept json
// @Produce json
// @param Authorization header string true "Authorization header"
// @param DeleteProductRequest body DeleteProductRequest true "sku of product"
// @Success 200 {object} api_helper.Response
// @Failure 400 {object} api_helper.ErrorResponse
// @Router /product [delete]
func (c *Controller) DeleteProduct(g *gin.Context) {
	var req DeleteProductRequest
	if err := g.ShouldBind(&req); err != nil {
		api_helper.HandleError(g, err)
		return
	}
	g.JSON(http.StatusOK, api_helper.Response{
		Message: "Product Deleted",
	})
}

// UpdateProduct 更新商品
// @Summary 更新商品根据sku
// @Tags Product
// @Accept json
// @Produce json
// @param Authorization header string true "Authorization header"
// @param UpdateProductRequest body UpdateProductRequest true "product information"
// @Success 200 {object} CreateProductResponse
// @Failure 400 {object} api_helper.ErrorResponse
// @Router /product [patch]
func (c *Controller) UpdateProduct(g *gin.Context) {
	var req UpdateProductRequest
	if err := g.ShouldBind(&req); err != nil {
		api_helper.HandleError(g, err)
		return
	}
	err := c.productService.UpdateProduct(req.ToProduct())
	if err != nil {
		api_helper.HandleError(g, err)
		return
	}
	g.JSON(http.StatusOK, CreateProductResponse{
		Message: "Product Updated",
	})

}
